package com.example.login;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText userNameET, pwdET1, pwdET2,memoET;
    RadioGroup radioGroup1;
    CheckBox cb1, cb2, cb3, cb4;
    ImageButton submitBtn, resetBtn;
    String hobby="";//暂存兴趣爱好
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c4_1_activity_main);
        userNameET = (EditText)findViewById(R.id.c41userNameET);
        pwdET1 =(EditText)findViewById(R.id.c41pwdET1);
        pwdET2 =(EditText)findViewById(R.id.c41pwdET2);
        memoET=(EditText)findViewById(R.id.c41memoET);
        radioGroup1=(RadioGroup)findViewById(R.id.c41radioGroup1);
        cb1=(CheckBox)findViewById(R.id.c41checkBox1);
        cb2=(CheckBox)findViewById(R.id.c41checkBox2);
        cb3=(CheckBox)findViewById(R.id.c41checkBox5);
        cb4=(CheckBox)findViewById(R.id.c41checkBox6);
        submitBtn = (ImageButton)findViewById(R.id.c41submitBtn);
        resetBtn =(ImageButton)findViewById(R.id.c41resetBtn);
        //为四个复选框注册通用的选中变化监听器
        cb1.setOnCheckedChangeListener(ocListener);
        cb2.setOnCheckedChangeListener(ocListener);
        cb3.setOnCheckedChangeListener(ocListener);
        cb4.setOnCheckedChangeListener(ocListener);
        //恢复初始状态
        resetBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                userNameET.setText("");
                pwdET1.setText("");
                pwdET2.setText("");
                radioGroup1.check(R.id.c41radio1);
                cb1.setChecked(false);
                cb2.setChecked(false);
                cb3.setChecked(false);
                cb4.setChecked(false);
                hobby="";
                memoET.setText("");
            }
        });
        submitBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                String userName= userNameET.getText().toString();
                String pwd1=pwdET1.getText().toString();
                String pwd2=pwdET2.getText().toString();
                int gid =radioGroup1.getCheckedRadioButtonId();
                RadioButton rb = (RadioButton)findViewById(gid);
                String sex= rb.getText().toString();
                String memo = memoET.getText().toString();
                if(userName.equals("")){
                    Toast.makeText(MainActivity.this,"用户名不能空",Toast.LENGTH_SHORT).show();
                    return ;
                }
                if(pwd1.equals("")){
                    Toast.makeText(MainActivity.this,"密码不能空",Toast.LENGTH_SHORT).show();
                    return ;
                }
                if(!pwd1.equals(pwd2)){
                    Toast.makeText(MainActivity.this,"两次密码不同",Toast.LENGTH_SHORT).show();
                    return ;
                }
                StringBuffer sb = new StringBuffer();
                sb.append("您的注册信息如下：\n");
                sb.append("  用户名：").append(userName).append("\n");
                sb.append("  密码：").append(pwd1).append("\n");
                sb.append("  性别：").append(sex).append("\n");
                sb.append("  兴趣爱好：").append(hobby).append("\n");
                sb.append("  个人简介：").append(memo);
                Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }
    OnCheckedChangeListener ocListener=new OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton cb, boolean isChecked){
            //获取被点击的复选框的值
            String s = cb.getText().toString();
            if(isChecked)
                hobby+="    "+s;//如果被选中，则hobby加上该选项
            else{
                String temp="    "+s;
                hobby=hobby.replaceAll(temp, "");//如取消选中，则删除该项。
            }
            Toast.makeText(MainActivity.this, hobby, Toast.LENGTH_LONG).show();
        }
    };
}
